package com.bsh.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoAccount;
import com.bsh.repository.DaoAccountLog;
import com.bsh.repository.DaoAccountNotice;
import com.bsh.repository.DaoPayment;
import com.bsh.repository.DaoUser;
import com.bsh.repository.DaoUserVerifyInfo;
import com.bsh.service.AccountService;
import com.bsh.service.ApiCommonService;
import com.bsh.service.IndexImageService;
import com.bsh.service.UserService;
import com.bsh.service.UserVerifyService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
import com.bsh.utils.Des3;
import com.bsh.utils.DesUtils;
import com.bsh.utils.Pager;
import com.bsh.utils.UploadUtils;

@Service
public class ApiCommonServiceImpl implements ApiCommonService {
	@Autowired
	IndexImageService indexImageService;
	@Autowired
	UserService servUser;
	@Autowired
	AccountService accountServ;
	@Autowired
	DaoUserVerifyInfo verifyInfoDao;
	@Autowired
	UserVerifyService userVerifyService;
	@Autowired
	DaoAccountNotice daoNotice;
	@Autowired
	DaoPayment daoPay;
	@Autowired
	DaoAccountLog daoAccLog;
	@Autowired
	DaoAccount daoAcc;
	@Autowired
	DaoUser daoUser;

	@Override
	public ApiMessage getIndexImages(JSONObject dto) throws ApiException {
		Pager pager = new Pager(1, 10);
		List<Map<String, Object>> list = indexImageService.getPage(pager, null);
		if (list.size() > 0) {
			String url = null;
			for (Map<String, Object> map : list) {
				url = (String) map.get("img");
				map.remove("img");
				map.put("img", dto.getString("serv_ip") + url);
			}
		}
		ApiMessage msg = new ApiMessage("200", "查询成功");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("indexImages", list);
		msg.setData(Des3.encResponse(data));
		return msg;
	}

	@Override
	public ApiMessage getMerchantIndexImages(JSONObject dto) throws ApiException {
		Pager pager = new Pager(1, 10);
		List<Map<String, Object>> list = indexImageService.getMerchantPage(pager, null);
		if (list.size() > 0) {
			String url = null;
			for (Map<String, Object> map : list) {
				url = (String) map.get("img");
				map.remove("img");
				map.put("img", dto.getString("serv_ip") + url);
			}
		}
		ApiMessage msg = new ApiMessage("200", "查询成功");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("merchantImages", list);
		msg.setData(Des3.encResponse(data));
		return msg;
	}

	@Override
	public ApiMessage isMerchant(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		ApiUtils.checkUserNameToken(dto);
		User user = servUser.getUserByName(dto.getString("userName"));
		if (ApiUtils.objIsEmpty(user)) {
			msg = new ApiMessage("102", "用户不存在");
			return msg;
		}
		JSONObject data = new JSONObject();
		int isMerchantValue = -1; // 默认-1 ：不是商家
		if (user.getIsMerchant() != null) {
			isMerchantValue = user.getIsMerchant().intValue();
		}
		data.put("isMerchant", isMerchantValue);
		msg = new ApiMessage("200", "查询成功");
		msg.setData(data.toString());
		return msg;
	}

	@Override
	public ApiMessage getUserVerifyInfo(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		ApiUtils.checkUserNameToken(dto);
		User user = servUser.getUserByName(dto.getString("userName"));
		if (ApiUtils.objIsEmpty(user)) {
			msg = new ApiMessage("102", "用户不存在");
			return msg;
		}
		JSONObject result = userVerifyService.getUserVerifyInfo(user.getId());
		if (ApiUtils.objIsNotEmpty(result)) {
			if (!result.getString("pic_url").equals("-1")) {
				result.put("pic_url", dto.getString("serv_ip") + result.getString("pic_url"));
			}
			if (ApiUtils.objIsEmpty(user.getCreateTime()) || ApiUtils.checkTime(user.getCreateTime())) {
				result.put("is_before", true);
			} else {
				result.put("is_before", false);
			}
			msg.setCode(Constants.REQ_SUCCESS);
			msg.setMessage(Constants.MSG_SUCCESS);
			msg.setData(Des3.encResponse(result));
		} else {
			throw new ApiException("109", "未实名认证");
		}
		return msg;
	}

	@Override
	public ApiMessage uploadPic(JSONObject dto, MultipartFile uploadPic) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String picUrl = "";
		if (!uploadPic.isEmpty()) {
			picUrl = UploadUtils.save(uploadPic, "/upload/private/merchant/pic");
			picUrl = dto.getString("serv_ip") + picUrl;
			msg = new ApiMessage("200", "图片上传成功");
			JSONObject json = new JSONObject();
			json.put("picUrl", picUrl);
			msg.setData(json.toJSONString());
		} else {
			msg = new ApiMessage("400", "请上传图片");
		}
		return msg;
	}

	@Override
	public ApiMessage queryPage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String result = "";
		try {
			result = indexImageService.getMyPage(dto.getString("page")).toString();
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(result);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage updatePage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		try {
			indexImageService.updateMyPage(dto.getString("page"));
		} catch (Exception e) {
			msg.setData(e.getMessage());
			msg.setCode("400");
			msg.setMessage("查询失败");
			return msg;
		}
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage updatePages(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		try {
			JSONArray pageList = dto.getJSONArray("pages");
			indexImageService.updateMyPageBatch(pageList);
		} catch (Exception e) {
			msg.setData(e.getMessage());
			msg.setCode("400");
			msg.setMessage("查询失败");
			return msg;
		}
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage addMerchant(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
		if (ApiUtils.objIsEmpty(dto.getString("mobile"))) {
			msg = new ApiMessage("400", "请填写手机号");
		}
		if (ApiUtils.objIsEmpty(dto.getString("password"))) {
			msg = new ApiMessage("400", "请填写密码");
		}
		if (msg == null) {
			User userCheckName = servUser.checkUserByNameOrMobile(dto.getString("mobile"));
			if (userCheckName != null) {
				msg = new ApiMessage("400", dto.getString("mobile") + " 已被使用");
				return msg;
			}
			User userReg = new User();
			userReg.setName(dto.getString("mobile"));
			DesUtils des = new DesUtils();
			String password = des.encryptPassword(dto.getString("password"));
			userReg.setPassword(password);
			userReg.setMobile(dto.getString("mobile"));
			userReg.setIsEffect(1);
			userReg.setCreateTime(dto.getDate("date"));
			userReg.setSex(dto.getString("sex"));
			userReg.setLevel("comm");
			User user = servUser.registerUser(userReg);
			if (user != null) {
				accountServ.newAccountByUser(user.getId());
				msg = new ApiMessage("200", "添加成功");
			} else {
				msg = new ApiMessage("102", "用户或密码错误");
			}
		}
		return msg;
	}

	@Override
	public ApiMessage queryUser(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String data = null;
		if (ApiUtils.objIsEmpty(dto.getString("page"))) {
			throw new ApiException("400", "入参不能为空");
		}
		try {
			data = indexImageService.getUser(dto.getString("page"));
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(data);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage queryUserPage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String data = null;
		if (ApiUtils.objIsEmpty(dto.getString("page"))) {
			throw new ApiException("400", "入参不能为空");
		}
		try {
			data = indexImageService.getUserPage(dto.getString("page"));
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(data);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage updateUserPage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String data = null;
		if (ApiUtils.objIsEmpty(dto.getString("page"))) {
			throw new ApiException("400", "入参不能为空");
		}
		try {
			data = indexImageService.updatePage(dto.getString("page"));
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(data);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage insertUserPage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String data = null;
		if (ApiUtils.objIsEmpty(dto.getString("page"))) {
			throw new ApiException("400", "入参不能为空");
		}
		try {
			data = indexImageService.insertPage(dto.getString("page"));
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(data);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage deleteUserPage(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage();
		String data = "";
		if (ApiUtils.objIsEmpty(dto.getString("page"))) {
			throw new ApiException("400", "入参不能为空");
		}
		try {
			data = indexImageService.deletePage(dto.getString("page"));
		} catch (Exception e) {
			throw new ApiException("400", e.getMessage());
		}
		msg.setData(data);
		msg.setCode("200");
		msg.setMessage("查询成功");
		return msg;
	}

	@Override
	public ApiMessage getMerchantDetail(JSONObject dto) throws ApiException {
		ApiMessage msg = new ApiMessage("200", "查询成功");
		return msg;
	}

}
